<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('qusstionnaire:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户id">
                <a-input v-model="queryParam.userId" allow-clear placeholder="请输入用户id"/>
              </a-form-item>
            </a-col>
<!--            <a-col :md="8" :sm="24">-->
<!--              <a-form-item label="问卷json">-->
<!--                <a-input v-model="queryParam.wjJson" allow-clear placeholder="请输入问卷json"/>-->
<!--              </a-form-item>-->
<!--            </a-col>-->
<!--            <template v-if="advanced">-->
              <a-col :md="8" :sm="24">
                <a-form-item label="审核状态">
<!--                  <a-input v-model="queryParam.examine" allow-clear placeholder="未审核:0,已审核:1,已驳回:2"/>-->
                  <a-select style="width: 100%" v-model = "queryParam.examine" placeholder="请选择审核状态" allow-clear>
                    <a-select-option value="0"> 未审核 </a-select-option>
                    <a-select-option value="1"> 已审核 </a-select-option>
                    <a-select-option value="2"> 已驳回 </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="审核人">
                  <a-input v-model="queryParam.examineUser" allow-clear placeholder="请输入审核人"/>
                </a-form-item>
              </a-col>
<!--              <a-col :md="8" :sm="24">-->
<!--                <a-form-item label="审核时间">-->
<!--                  <a-date-picker style="width: 100%" placeholder="请选择审核时间" v-model="queryParam.examineTimeDate" @change="onChangeexamineTime"/>-->
<!--                </a-form-item>-->
<!--              </a-col>-->
<!--            </template>-->
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
<!--                <a @click="toggleAdvanced" style="margin-left: 8px">-->
<!--                  {{ advanced ? '收起' : '展开' }}-->
<!--                  <a-icon :type="advanced ? 'up' : 'down'"/>-->
<!--                </a>-->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('qusstionnaire:add')" >
<!--          <a-button type="primary" v-if="hasPerm('qusstionnaire:add')" icon="plus" @click="$refs.addForm.add()">新增用户问卷</a-button>-->
<!--          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('qusstionnaire:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>-->
          <x-down
            v-if="hasPerm('qusstionnaire:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('qusstionnaire:edit')" @click="$refs.editForm.edit(record)">审核</a>
<!--          <a-divider type="vertical" v-if="hasPerm('qusstionnaire:edit') & hasPerm('qusstionnaire:delete')"/>-->
<!--          <a-popconfirm v-if="hasPerm('qusstionnaire:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">-->
<!--&lt;!&ndash;            <a>删除</a>&ndash;&gt;-->
<!--          </a-popconfirm>-->
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />

    </a-card>
      <a-modal width="800px" v-model="showAns" title="作答详情" @ok="showAns=false">
     <div style="min-height:60px;margin-bottom:20px" v-for="(item,index) in ansData" :key="index">
       <div>
       <span style="font-size:16px">题目名称:{{item.title}}{{item.answerMethod}}</span>
       </div>
       <div style="display:flex" v-show="+item.answerMethod === 0">
         <span style="font-size:16px">选项：</span>
         <p style="margin-left:5px;font-size:16px" v-for="(items,indexs) in item.answer" :key="indexs">{{items.label}}:{{items.value}}</p>
         </div>
         <span style="font-size:16px">作答结果:</span>
         <span v-show="+item.answerMethod === 0">{{item.write.toString()}} </span>
         <div v-show="+item.answerMethod === 1">
           <img style="width:200px;height:200px" v-for="(img,imgIndex) in item.write" :key="imgIndex" :src="`http://qiuniuyun.xiongxiaoji.com/${img.url}`" >
         </div>
     </div>
    </a-modal>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import moment from 'moment'
  import { qusstionnairePage, qusstionnaireDelete, qusstionnaireExport } from '@/api/modular/main/qusstionnaire/qusstionnaireManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
import { mapGetters } from 'vuex'

  export default {
    components: {
      STable,
      addForm,
      editForm,
      XDown
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '用户ID',
            align: 'center',
            dataIndex: 'userId'
          },
          {
            title: '提交时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '问卷详细',
            align: 'center',
            dataIndex: 'wjJson',
                         customRender: (value, row, index) => {
   if(JSON.parse(value).length){
     return   <a-button type="primary" onClick={()=>{this.getAns(value)}}>
      查看详情
    </a-button>
   }else{
    return <span style="color:red">未作答</span>
   }
        },
          },
          {
            title: '审核状态',
            align: 'center',
            dataIndex: 'examine',
              customRender: (value, row, index) => {

            return <span>{value === 1?'已审核':value === 2?'已驳回':"未审核"}</span>;
        },
          },
          {
            title: '审核人',
            align: 'center',
            dataIndex: 'examineUser'
          },
          {
            title: '审核时间',
            align: 'center',
            dataIndex: 'examineTime'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return qusstionnairePage(Object.assign(parameter, this.switchingDate())).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        showAns:false,
        ansData:[]
      }
    },
    created () {
      if (this.hasPerm('qusstionnaire:edit') || this.hasPerm('qusstionnaire:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      getAns(data){
        this.showAns = false
        this.ansData.length = 0
        if(data){
          this.ansData = JSON.parse(data)
          this.showAns = true
          console.log(this.ansData)
        }
      },
      moment,
      /**
       * 查询参数组装
       */
      switchingDate () {
        const queryParamexamineTime = this.queryParam.examineTimeDate
        if (queryParamexamineTime != null) {
            this.queryParam.examineTime = moment(queryParamexamineTime).format('YYYY-MM-DD')
            if (queryParamexamineTime.length < 1) {
                delete this.queryParam.examineTime
            }
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        if(obj.examineTimeDate){
          delete  obj.examineTimeDate
        }
        console.log(obj,1111)
        return obj
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.qusstionnaireDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.qusstionnaireDelete(paramIds)
      },
      qusstionnaireDelete (record) {
        qusstionnaireDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChangeexamineTime(date, dateString) {
        this.examineTimeDateString = dateString
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        qusstionnaireExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
